package com.hbu.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @ProjectName: hbu-mall
 * @Package: com.hbu.common.valid
 * @ClassName: ListValueConstraintValidator
 * @Author: zxh
 * @Description:
 * @Date: 2022/11/12 18:51
 * @Version: 1.0
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {

    private final Set<Integer> valueSet = new HashSet<>(8);

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return valueSet.contains(value);
    }

    @Override
    public void initialize(ListValue constraintAnnotation) {
        Arrays.stream(constraintAnnotation.values()).forEach(valueSet::add);
    }
}
